package com.springmvc.controller;

import com.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ScopeParamController {

    @RequestMapping("/request_param")
    public String request_param(HttpServletRequest request){
        request.setAttribute("userName","张三");
        return "param";
    }

    @RequestMapping("/mv_param")
    public ModelAndView mv_param(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("param");//视图名称
        mv.addObject("userName","张三");//给页面传递参数
        mv.addObject("user",new User(1L,"李四","123456",22D));

        return mv;//返回视图
    }

    @RequestMapping("/model_param")
    public String model_param(Model model,String userName){
        model.addAttribute("userName",userName);
        model.addAttribute("user",new User(1L,"李四","123456",22D));
        model.addAttribute("password","默认密码。。。");

        return "param";
    }

    @RequestMapping("/map_param")
    public String map_param(Map<String,Object> map, String userName){
        map.put("mapUserName",userName);
        map.put("mapUser",new User(1L,"李四","123456",22D));
        map.put("mapPassword","默认密码。。。");

        return "param";
    }

    @RequestMapping("/modelMap_param")
    public String modelMap_param(ModelMap modelMap, String userName){
        modelMap.addAttribute("mapUserName",userName);
        modelMap.addAttribute("mapUser",new User(1L,"李四","123456",22D));
        modelMap.addAttribute("mapPassword","默认密码。。。");

        return "param";
    }

    @RequestMapping("/session_param")
    public String session_param(HttpSession session,String userName){
        session.setAttribute("sessionUserName",userName);
        session.setAttribute("sessionUser",new User(1L,"李四","123456",22D));
        session.setAttribute("sessionPassword","默认密码。。。");
        return "param";
    }

    @RequestMapping("/application_param")
    public String application_param(HttpServletRequest request,String userName){
        ServletContext context = request.getServletContext();
        context.setAttribute("applicationUserName",userName);
        context.setAttribute("applicationUser",new User(1L,"李四","123456",22D));
        context.setAttribute("applicationPassword","默认密码。。。");

        return "param";
    }

}
